package com.laolu.icon.utils;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.laolu.icon.constants.ShortcutProperty;
import com.laolu.icon.entity.IconInfo;
import org.jetbrains.annotations.NotNull;

public class ShortcutUtils implements AutoCloseable {
	
	private final ActiveXComponent shell;
	
	public ShortcutUtils() {
		shell = new ActiveXComponent("WScript.Shell");
	}
	
	/**
	 * 获取快捷方式信息
	 * @param iconInfo
	 * 		快捷方式信息(必须包含全路径)
	 * @return 快捷方式全部信息
	 */
	public IconInfo getShortcutInfo(@NotNull IconInfo iconInfo) {
		// 打开 .lnk 文件
		Dispatch shortcut = Dispatch.call(shell, ShortcutProperty.CREATE_SHORTCUT.toString(), iconInfo.getFullName()).toDispatch();
		
		//目标文件路径
		String targetPath = get(shortcut, ShortcutProperty.TARGET_PATH).getString();
		iconInfo.setTargetPath(targetPath);
		//图标路径和索引
		String iconLocation = get(shortcut, ShortcutProperty.ICON_LOCATION).getString();
		iconInfo.setIconLocation(iconLocation);
		//工作目录
		String workingDirectory = get(shortcut, ShortcutProperty.WORKING_DIRECTORY).getString();
		iconInfo.setWorkingDirectory(workingDirectory);
		//命令行参数
		String arguments = get(shortcut, ShortcutProperty.ARGUMENTS).getString();
		iconInfo.setArguments(arguments);
		//描述文本
		String description = get(shortcut, ShortcutProperty.DESCRIPTION).getString();
		iconInfo.setDescription(description);
		//启动窗口样式
		int windowStyle = get(shortcut, ShortcutProperty.WINDOW_STYLE).getInt();
		iconInfo.setWindowStyle(windowStyle);
		//快捷键组合
		String Hotkey = get(shortcut, ShortcutProperty.HOTKEY).getString();
		iconInfo.setHotkey(Hotkey);
		//相对路径
//		String RelativePath = get(shortcut, ShortcutProperty.RELATIVE_PATH).getString();
//		shortcutInfo.setRelativePath(RelativePath);
		
		// 释放资源
		shortcut.safeRelease();
		
		return iconInfo;
	}
	
	/**
	 * 获取快捷方式信息
	 * @param lnkPath
	 * 		快捷方式全路径
	 * @return 快捷方式全部信息
	 */
	public IconInfo getShortcutInfo(String lnkPath) {
		IconInfo shortcut = new IconInfo();
		shortcut.setFullName(lnkPath);
		return getShortcutInfo(shortcut);
	}
	
	/**
	 * 创建快捷方式
	 * @param shortcutInfo
	 * @return
	 */
	public Variant createShortcut(@NotNull IconInfo shortcutInfo) {
		Dispatch shortcut = null;
		try {
			// 创建快捷方式
			shortcut = Dispatch.call(shell, ShortcutProperty.CREATE_SHORTCUT.toString(), shortcutInfo.getFullName()).toDispatch();
			
			// 设置目标路径
			Dispatch.put(shortcut, ShortcutProperty.TARGET_PATH.toString(), new Variant(shortcutInfo.getTargetPath()));
			
			// 设置图标位置
			if (shortcutInfo.getIconLocation() != null) {
				Dispatch.put(shortcut, ShortcutProperty.ICON_LOCATION.toString(), new Variant(shortcutInfo.getIconLocation()));
			}
			
			// 设置工作目录
			if (shortcutInfo.getWorkingDirectory() != null) {
				Dispatch.put(shortcut, ShortcutProperty.WORKING_DIRECTORY.toString(), new Variant(shortcutInfo.getWorkingDirectory()));
			}
			
			// 设置命令行参数
			if (shortcutInfo.getArguments() != null) {
				Dispatch.put(shortcut, ShortcutProperty.ARGUMENTS.toString(), new Variant(shortcutInfo.getArguments()));
			}
			
			// 设置描述
			if (shortcutInfo.getDescription() != null) {
				Dispatch.put(shortcut, ShortcutProperty.DESCRIPTION.toString(), new Variant(shortcutInfo.getDescription()));
			}
			
			// 设置窗口样式
			Dispatch.put(shortcut, ShortcutProperty.WINDOW_STYLE.toString(), new Variant(shortcutInfo.getWindowStyle()));
			
			// 设置快捷键
			if (shortcutInfo.getHotkey() != null) {
				Dispatch.put(shortcut, ShortcutProperty.HOTKEY.toString(), new Variant(shortcutInfo.getHotkey()));
			}
			
			// 保存快捷方式
			Variant save = Dispatch.call(shortcut, "Save");
			return save;
		} finally {
			// 释放资源
			if (shortcut != null) {
				shortcut.safeRelease();
			}
		}
		
	}
	
	
	private Variant get(Dispatch shortcut, @NotNull ShortcutProperty property) {
		return Dispatch.get(shortcut, property.toString());
	}
	
	@Override
	public void close() {
		shell.safeRelease();
	}
}
